package com.hm.service.impl.task;

import com.hm.service.entity.ydcg.TaskDetailsBean;
import com.hm.service.framework.base.BaseServiceImpl;
import com.hm.service.mapper.task.TaskDetailsMapper;
import com.hm.service.service.main.TaskDetailsService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
public class TaskDetailsServiceImpl extends BaseServiceImpl implements TaskDetailsService {

    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    public TaskDetailsServiceImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public void saveTask(String taskId, String jobKey, String jobClassName, String cronExpression, String status, String description) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            TaskDetailsMapper mapper = session.getMapper(TaskDetailsMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("taskId", taskId);
            params.put("jobKey", jobKey);
            params.put("jobClassName", jobClassName);
            params.put("cronExpression", cronExpression);
            params.put("status", status);
            params.put("description", description);
            mapper.saveTask(params);
        }
    }

    @Override
    public void deleteTask(String taskId) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            TaskDetailsMapper mapper = session.getMapper(TaskDetailsMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("taskId", taskId);
            mapper.deleteTask(params);
        }
    }

    @Override
    public List<TaskDetailsBean> queryAllTask() {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            TaskDetailsMapper mapper = session.getMapper(TaskDetailsMapper.class);
            return mapper.queryAllTask();
        }
    }

    @Override
    public void updateTask(String taskId, String cronExpression) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            TaskDetailsMapper mapper = session.getMapper(TaskDetailsMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("taskId", taskId);
            params.put("cronExpression", cronExpression);
            mapper.updateTask(params);
        }
    }
}
